<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
		<style type="text/css">
			body,
			html,
			#allmap {
				width: 100%;
				height: 100%;
				overflow: hidden;
				margin: 0;
				font-family: "微软雅黑";
			}
		</style>
		<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=dAVCgTnW50hRWsp1bXRfPLPIMLF1nR3b"></script>
		<title>添加行政区划</title>
		<!--加载鼠标绘制工具-->
		<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
		<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
		<script type="text/javascript" src="js/myUtils.js"></script>
		<script type="text/javascript" src="js/myCalc.js"></script>
	</head>
	<body>
		<div id="allmap"></div>
	</body>
</html>
<script type="text/javascript">
	var ply; //记录区域绘制图形
	var line; //记录每次的线
	var linePoint; //当前点击折线的点
	var plyPoint; //点击多边形的点
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
	map.enableScrollWheelZoom();
	//显示规定区域边界
	function getBoundary() {
		var bdary = new BMap.Boundary();
		bdary.get("济南", function(rs) { //获取行政区域
			map.clearOverlays(); //清除地图覆盖物       
			var count = rs.boundaries.length; //行政区域的点有多少个
			if (count === 0) {
				alert('未能获取当前输入行政区域');
				return;
			}
			var pointArray = [];
			for (var i = 0; i < count; i++) {
				ply = new BMap.Polygon(rs.boundaries[i], {
					strokeWeight: 2,
					strokeColor: "#ff0000"
				}); //建立多边形覆盖物
				map.addOverlay(ply); //添加覆盖物
				pointArray = pointArray.concat(ply.getPath());
			}
			map.setViewport(pointArray); //调整视野                
		});
	}
	setTimeout(function() {
		getBoundary();
	}, 1000);

	//点击拾取坐标点
	function AddClick() {
		//单击获取点击的经纬度
		map.addEventListener("click", AddOrRemove);

	}
	// 拾取坐标的通用方法
	function AddOrRemove(e) {
		alert(e.point.lng + "," + e.point.lat);
		clickCentPoint = new BMap.Point(e.point.lng, e.point.lat);
		Addpoint();
	}
	//添加点
	function Addpoint(point) {
		//添加中心点的说明				

		var label = new BMap.Marker(point); // 创建文本标注对象		
		map.addOverlay(label);
	}
	var overlays = [];
	//回调获得覆盖物信息
	var overlaycomplete = function(e) {
		overlays.push(e.overlay);

		var result; //记录覆盖物数量
		if (e.drawingMode == BMAP_DRAWING_MARKER) {
			//开启拖拽
			e.overlay.enableDragging();
			result = ' 坐标：' + e.overlay.getPosition().lng + ',' + e.overlay.getPosition().lat;
		}
		if (e.drawingMode == BMAP_DRAWING_POLYLINE || e.drawingMode == BMAP_DRAWING_POLYGON || e.drawingMode ==
			BMAP_DRAWING_RECTANGLE) {
			line = e.overlay;
			//开启多边形编辑
			e.overlay.enableEditing();
			//result += ' 所画的点个数：' + e.overlay.getPath().length;
			result = e.overlay.getPath();
			//给折线添加事件
			//line.addEventListener('click', clickLine);
		}
		console.log("所画的点")
		console.log(result) //将绘制的覆盖物信息结果输出到结果面板
	};

	var styleOptions = {
		strokeColor: "red", //边线颜色。
		fillColor: "red", //填充颜色。当参数为空时，圆形将没有填充效果。
		strokeWeight: 2, //边线的宽度，以像素为单位。
		strokeOpacity: 0, //边线透明度，取值范围0 - 1。
		fillOpacity: 0.6, //填充的透明度，取值范围0 - 1。
		strokeStyle: 'solid', //边线的样式，solid或dashed。


	}
	//实例化鼠标绘制工具
	var drawingManager = new BMapLib.DrawingManager(map, {
		isOpen: false, //是否开启绘制模式
		enableDrawingTool: true, //是否显示工具栏
		drawingToolOptions: {
			anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
			offset: new BMap.Size(5, 5), //偏离值
			scale: 0.8, //工具栏缩放比例
			drawingModes: [BMAP_DRAWING_MARKER, BMAP_DRAWING_POLYLINE]
		},
		polylineOptions: styleOptions, //线的样式

	});
	//drawingManager.setDrawingMode(BMAP_DRAWING_POLYLINE);

	//添加鼠标绘制工具监听事件，用于获取绘制结果
	drawingManager.addEventListener('overlaycomplete', overlaycomplete);

</script>
